<?php

namespace App\Http\Controllers\Admin\Examine;

use App\Http\Controllers\Controller;
use App\Models\AdminModel;
use App\Models\examine\ConfigModel;
use App\Models\examine\RecordCopyModel;
use App\Models\examine\RecordDetailModel;
use App\Models\examine\RecordModel;
use App\Models\StaffModel;
use Illuminate\Http\Request;
use App\Exceptions\AppException;

class Record extends Controller
{

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $update_start = $request->input('update_start', '');
        $update_end = $request->input('update_end', '');

        $query = ConfigModel::query();


        if($update_start && $update_end){
            $query = $query->whereBetween('updated_at', [$update_start, $update_end]);
        }

        $items = $query->get();



        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'type' => $item->type,
                'examine_type' => $item->examine_type,
                'examine_staff_id' => $item->examine_staff_id ? json_decode($item->examine_staff_id, true) : [],
                'copy_staff_id' => $item->copy_staff_id ? json_decode($item->copy_staff_id, true) : [],
                'updated_at' => $item->updated_at,
            ];
            return $res;
        });

        return $this->success(['list' => $list]);
    }

    /**
     * Notes:添加审核记录和详情
     * User: xiangzi
     * Date: 2024/9/13
     * Time: 14:18
     * @return mixed
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function add($type) {
        $auth = \request()->get('auth');
        $examine_config = ConfigModel::where('type', $type)->first();
        if($examine_config){
            if(!$examine_config['examine_staff_id']){
                throw new AppException('审核流程还未设置');
            }
            $examine_staff_id = json_decode($examine_config['examine_staff_id'], true);
            $copy_staff_id = json_decode($examine_config['copy_staff_id'], true);
            $examine_no = getCode('examine', 'SHDH', 5);

            $model = new RecordModel();
            $model->config_id = $examine_config['id'];
            $model->type = $type;
            $model->examine_type = $examine_config['examine_type'];
            $model->examine_no = $examine_no;
            $model->created_admin_id = $auth->id;
            $model->save();

            $detail_data = []; $copy_data = [];
            foreach ($examine_staff_id as $key => $value) {
                $is_examine = 1;
                //如果是依次审批，第一个人可以审批，后面的暂时不可审批
                if($model->examine_type == 1){
                    if($key > 0) $is_examine = 0;
                }

                $admin_id = AdminModel::where('staff_id', $value['staff_id'])->value('id');
                if(!$admin_id){
                    throw new AppException('审批员工需要对应的管理后台账号');
                }

                $detail_data[] = [
                    'type' => $type,
                    'record_id' => $model->id,
                    'staff_id' => $value['staff_id'],
                    'sort' => isset($value['sort']) ? $value['sort'] : $key + 1,
                    'admin_id' => $admin_id,
                    'created_admin_id' => $auth->id,
                    'is_examine' => $is_examine,
                    'created_at' => date('Y-m-d H:i:s', time()),
                ];
            }

            foreach ($copy_staff_id as $staff_id) {
                $copy_data[] = [
                    'type' => $type,
                    'record_id' => $model->id,
                    'staff_id' => $staff_id,
                    'admin_id' => AdminModel::where('staff_id', $staff_id)->value('id'),
                    'created_admin_id' => $auth->id,
                    'created_at' => date('Y-m-d H:i:s', time()),
                ];
            }

            $detail_model = new RecordDetailModel();
            $copy_model = new RecordCopyModel();
            $detail_model->insert($detail_data);
            $copy_model->insert($copy_data);

            return $model->id;

        }else{
            throw new AppException('审核流程设置不存在');
        }
    }


    /**
     * Notes:审核流程员工及其审核状态
     * User: xiangzi
     * Date: 2024/9/14
     * Time: 11:58
     * @return mixed
     */
    public function examine_info($id)
    {
        $details = RecordDetailModel::where('record_id', $id)->orderBy('sort')->get();
        $list = $details->map(function($item) {
            $staff_info = StaffModel::with('role')->find($item->staff_id);
            $res = [
                'staff_name' => $staff_info ? $staff_info->name : '',
                'staff_role' => $staff_info ? $staff_info->role->name : '',
                'status' => $item->status,
                'status_text' => RecordModel::STATUS[$item->status],
                'examine_time' => $item->examine_time,
                'examine_remarks' => $item->examine_remarks,
            ];
            return $res;
        });

        return $list;
    }

    /**
     * Notes:审核抄送人员及其状态
     * User: xiangzi
     * Date: 2024/9/18
     * Time: 10:24
     * @return mixed
     */
    public function copy_info($id)
    {
        $details = RecordCopyModel::where('record_id', $id)->get();
        $list = $details->map(function($item) {
            $staff_info = StaffModel::with('role')->find($item->staff_id);
            if(!$staff_info){
                $res = [
                    'staff_name' => '',
                    'staff_role' => '',
                    'status' => $item->status,
                    'status_text' => RecordCopyModel::STATUS[$item->status],
                ];
            }else{
                $res = [
                    'staff_name' => $staff_info->name ?? '',
                    'staff_role' => $staff_info->role ? $staff_info->role->name : '',
                    'status' => $item->status,
                    'status_text' => RecordCopyModel::STATUS[$item->status],
                ];
            }

            return $res;
        });
        return $list;
    }

}
